#!/bin/bash

# SuperTokens 一键测试脚本

echo "🧪 SuperTokens 一键测试脚本"
echo "================================"

# 检查是否在项目根目录
if [ ! -f "run.py" ]; then
    echo "❌ 请在项目根目录运行此脚本"
    exit 1
fi

echo "📋 选择测试方案："
echo "1. 测试当前配置（检查现有设置）"
echo "2. 启用SuperTokens测试（使用免费测试服务）"
echo "3. 禁用SuperTokens（使用原有认证系统）"
echo "4. 查看详细测试指南"

read -p "请输入选项 (1-4): " choice

case $choice in
    1)
        echo ""
        echo "🔍 检查当前配置..."
        
        if [ -f ".env" ]; then
            echo "✅ 发现 .env 文件"
            if grep -q "SUPERTOKENS_CONN_URI" .env; then
                echo "✅ 发现SuperTokens配置"
                echo "当前配置："
                grep "SUPERTOKENS_" .env || echo "   (配置为空)"
            else
                echo "⚠️ 未发现SuperTokens配置，将使用原有认证系统"
            fi
        else
            echo "⚠️ 未发现 .env 文件，将使用默认配置"
        fi
        
        echo ""
        echo "🚀 启动应用进行测试..."
        echo "访问 http://localhost:8000 查看效果"
        python run.py
        ;;
        
    2)
        echo ""
        echo "🔧 配置SuperTokens测试环境..."
        
        # 备份现有.env文件
        if [ -f ".env" ]; then
            cp .env .env.backup
            echo "✅ 已备份现有 .env 文件为 .env.backup"
        fi
        
        # 添加SuperTokens测试配置
        cat >> .env << EOF

# SuperTokens 测试配置 (自动添加)
SUPERTOKENS_CONN_URI=https://try.supertokens.com
SUPERTOKENS_API_KEY=
SUPERTOKENS_APP_NAME=yzs-ppt-test
SUPERTOKENS_API_DOMAIN=http://localhost:8000
SUPERTOKENS_WEBSITE_DOMAIN=http://localhost:8000
SUPERTOKENS_COOKIE_DOMAIN=localhost
EOF
        
        echo "✅ SuperTokens测试配置已添加到 .env 文件"
        echo ""
        echo "🚀 启动应用..."
        echo "请查看启动日志中的 'SuperTokens initialized successfully'"
        echo "然后访问 http://localhost:8000/auth/login 查看SuperTokens登录界面"
        python run.py
        ;;
        
    3)
        echo ""
        echo "🔧 禁用SuperTokens配置..."
        
        if [ -f ".env" ]; then
            # 注释掉SuperTokens配置
            sed -i.bak 's/^SUPERTOKENS_/#SUPERTOKENS_/g' .env
            echo "✅ SuperTokens配置已注释，应用将使用原有认证系统"
        else
            echo "✅ 未发现 .env 文件，应用将使用原有认证系统"
        fi
        
        echo ""
        echo "🚀 启动应用..."
        echo "应用将使用原有的认证系统"
        python run.py
        ;;
        
    4)
        echo ""
        echo "📖 详细测试指南："
        echo ""
        echo "1. 基础测试："
        echo "   python test_supertokens.py"
        echo ""
        echo "2. 手动测试URL："
        echo "   - http://localhost:8000/auth/login (登录页面)"
        echo "   - http://localhost:8000/dashboard (需要认证)"
        echo "   - http://localhost:8000/api/auth/check (认证状态API)"
        echo ""
        echo "3. 查看详细文档："
        echo "   - SUPERTOKENS_TESTING_GUIDE.md (测试指南)"
        echo "   - SUPERTOKENS_MIGRATION_GUIDE.md (迁移指南)"
        echo ""
        echo "4. SuperTokens配置示例："
        echo "   SUPERTOKENS_CONN_URI=https://try.supertokens.com"
        echo "   SUPERTOKENS_API_KEY="
        echo "   SUPERTOKENS_APP_NAME=yzs-ppt-test"
        echo "   SUPERTOKENS_API_DOMAIN=http://localhost:8000"
        echo "   SUPERTOKENS_WEBSITE_DOMAIN=http://localhost:8000"
        echo "   SUPERTOKENS_COOKIE_DOMAIN=localhost"
        ;;
        
    *)
        echo "❌ 无效选项，请重新运行脚本"
        exit 1
        ;;
esac
